<?php

/**
 * undocumented function
 *
 * @return void
 * @author Hugo Wetterberg
 **/
function theme_quest_submission_result(&$submission) {
  $node = node_load($submission->questionnaire);
  $class = 'quest-result';
  
  
  //Get grading info 
  $grading_field_name = variable_get('quest_grading_field_'. $node->type,'');
  $grade = '';
  $grade_points = -1;
  if ($grading_field_name) {
    foreach ($node->$grading_field_name as $grading_field) {
      $matches = array();
      if (preg_match('/^(\d+):\s*(.*)/', $grading_field['value'], $matches)) {
        $limit = intval($matches[1]);
        if ($limit>$grade_points && $submission->points>=$limit) {
          $grade = $matches[2];
          $grade_points = $limit;
        }
      }
    }
  }
  
  //Add some point related classes
  if ($submission->total) {
    switch ($submission->points) {
      case $submission->total:
        $class .= ' quest-result-nailed-it';
        break;
      case 0:
        $class .= ' quest-result-total-loss';
        break;
    }
  }
  
  $o .= '<div class="'. $class .'">';
  $o .=   '<h2 class="title">'. $node->title .'</h2>';
  $o .=   theme('quest_submission_total', $submission, $grade);
  $o .=   '<ol>';
  foreach($submission->answers as $question => $answer) {
    if ($answer['type']=='question') {
      $o .= theme('quest_questionnaire_answer_result', $submission, $question, $answer);
    }
  }
  $o .=   '</ol>';
  $o .= '</div>';
  return $o;
}

/**
 * undocumented function
 *
 * @return void
 * @author Hugo Wetterberg
 **/
function theme_quest_submission_total(&$submission, $grading='') {
  if (!$submission->total) {
    return '';
  }
  
  $average = votingapi_get_voting_result('node', $submission->questionnaire, 'points', 'quest_points', 'average');
  $user_percent = round($submission->points * 100 / $submission->total);
  $average_percent = round($average->value * 100 / $submission->total);
  
  $class = 'submission-total';
  if($user_percent<$average_percent) {
    $class .= ' result-below-average';
  }
  else if($user_percent>$average_percent) {
    $class .= ' result-above-average';
  }
  else {
    $class .= ' result-is-average';
  }
  
  $o = '<div class="'. $class .'">';
  $o .= '<p class="result-points">'. t('You scored @points of a total of @total!',array('@points'=>$submission->points, '@total'=>$submission->total)) .'</p>';
  if (!empty($grading)) {
    $o .= '<p class="result-grade">'. $grading .'</p>';
  }
  $o .= '<div class="result-percent">'. 
    theme('quest_percentage', $user_percent, t('Your rating'), array('class' => 'percentage-user')) .
    theme('quest_percentage', $average_percent, t('Average rating'), array('class' => 'percentage-average')) .'</div>';
  $o .= '</div>';
  return $o;
}

/**
 * undocumented function
 *
 * @return void
 * @author Hugo Wetterberg
 **/
function theme_quest_percentage($percentage, $label='', $attributes=array()) {
  if (!is_array($attributes)) {
    $attributes = array();
  }
  
  $short_percentage = floor($percentage/10);
  $attributes['class'] .= ' percentage percentage-'. $short_percentage;
  if ($short_percentage>=5) {
    $attributes['class'] .= ' percentage-gte-5';
  }
  else {
    $attributes['class'] .= ' percentage-lt-5';
  }
  $o = '<div '. drupal_attributes($attributes) .'>';
  if (!empty($label)) {
    $o .= '<label>'. check_plain($label) .'</label>';
  }
  $o .= '<div class="percent">'. sprintf('%d%%', $percentage) .'</div></div>';
  return $o;
}

/**
 * undocumented function
 *
 * @return void
 * @author Hugo Wetterberg
 **/
function theme_quest_questionnaire_answer_result(&$submission, $question, $answer) {
  $node = node_load($question);
  
  $class = 'answer';
  if (isset($answer['correct'])) {
    $class .= ' answer_'. $answer['correct']?'correct':'incorrect';
    $average = votingapi_get_voting_result('node', $submission->questionnaire, 'points', 'quest_p_'. $question, 'average');
    $average_percent = round($average->value * 100);
  }
  
  if (isset($answer['text'])) {
    $class .= ' answer_free_text';
  }
  
  $o .= '<li class="'. $class .'">';
  $o .=   '<h3>'. $node->title .'</h3>';
  $o .=   '<div class="answer_inner">';
  if (isset($answer['text'])) {
    if (empty($answer['text'])) {
      $o .=   '<label>'. t('You didn\'t write anything') .'</label>';
    }
    else {
      $o .=   '<label>'. t('You wrote:') .'</label>';
      $o .=   '<p class="reply">'. $answer['text'] .'</p>';
    }
  }
  
  if (isset($answer['correct'])) {
    $o .=   '<label>'. t('Your answer was') .' <span class="result">'. ($answer['answer']['correct']?t('correct'):t('wrong')) .'</span></label>';
  }
  
  if ($answer['subtype'] == 'free-options') {
    $o .= _quest_render_option_question_result($submission->questionnaire, $question, $answer);
  }
  
  if (isset($average_percent)) {
    $o .=   theme('quest_percentage', $average_percent, t('Average result'), array('class' => 'percentage-average'));
  }
  
  $o .=   '</div>';
  $o .= '</li>';
  return $o;
}

/**
 * Themes a free options result
 *
 * @param int $question The id of the question that was answered
 * @param array $answer The answer that was given
 * @param array $results The total results, as an array of vote cache entries
 * @return void
 * @author Hugo Wetterberg
 **/
function theme_quest_free_option_result($question, $results, $answer=Null) {
  $node = node_load($question);
  
  $sum = 0;
  foreach ($results as $r) {
    $sum += $r->value;
  }
  
  $o .= '<ol>';
  foreach ($results as $r) {
    $percent = round($r->value*100/$sum);
    $o .= '<li>'. $node->field_answers[$r->function]['value'] .' 
    <div class="option-vote-result">
      <div class="vote-inner"><span class="percent">'. $percent .'%</span></div>
      <div class="vote-percent" style="width: '. ($percent*2) .'px"><span class="percent">'. $percent .'%</span></div>
    </div></li>';
  }
  $o .= '</ol>';
  return $o;
}

/**
 * Theme function for questionnaires
 *
 * @param object $node Questionnaire node
 * @param string $questions The themed questions
 *
 * @return string Themed questionnaire
 * @author Hugo Wetterberg
 **/
function theme_quest_questionnaire(&$node, $questions) {
  $content = '<form class="questionnaire_form" method="post" action="'. url('quest/submission/'. $node->nid) .'">';
  $content .= $questions;
  $content .= '<div class="questionnaire_submit"><button type="submit">'. t('Send') .'</button></div>';
  $content .= '</form>';
  return $content;
}

/**
 * Theme function for answers
 *
 * @param object $node The question node
 * @param array $answers An array with answer info
 *
 * @return string Themed answers
 * @author Hugo Wetterberg
 **/
function theme_quest_answers(&$questionnaire, &$node, $answers) {
  $content = '<div class="answers">';
  if($node->quest->type == 'free-text') {
    $content .= '<div class="answer free-answer">';
    $content .= '<textarea id="answer_'. $node->nid .'" name="answer_'. $node->nid .'" rows="7"></textarea></div>';
  }
  else {
    foreach($answers as $answer_id => $info) {
      if (empty($info['text']) && !isset($info['image'])) {
        continue;
      }
      $id = 'answer_'. $node->nid .'_'. $answer_id;
      $content .= '<div class="answer"><div class="inner"><div class="answer-image"><label for="'. $id .'">'. $info['image'] .'</label></div><input type="radio" id="'. $id .'" name="answer_'. $node->nid .'" value="'. $answer_id .'"/><label for="'. $id .'" class="text">'. $info['text'] .'</label></div></div>';
    }
  }
  
  $content .= '</div>';
  
  if($questionnaire && $node->quest->type == 'free-options') {
    $content .= l('Visa resultat', 'quest/preview/'. $questionnaire->nid .'/'. $node->nid, array('rel'=>'result', 'class'=>'result'));
  }
  
  return $content;
}